# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         test05
# Description:  字典方法
# Author:       ASUS
# Date:         2019/3/4
#-------------------------------------------------------------------------------

# clear方法    清空字典中所有元素
names1 = {"Bill":20,"Mike":30,"John":40}
names2 = names1
print(names2)                                     #{'Bill': 20, 'Mike': 30, 'John': 40}
names1 ={}
print(names2)                                     #{'Bill': 20, 'Mike': 30, 'John': 40},不会清空names2
names1 = {"Bill":20,"Mike":30,"John":40}
names2 = names1
names1.clear()
print(names2)                                     #{}
# copy方法和deepcopy方法    copy修改字典中第一层元素，对应的值并不会改变，不管修改哪一个字典中的第二层元素，另一个字典对应的值都会改变
#deepcopy修改dict中第二层元素，newdict对应的值不会改变
persons1 = {"Name":"Bill","age":30,"fullname":["Bill","Gates"]}
persons2 = persons1.copy()
print("persons1",persons1)
print("persons2",persons2)
persons2['age'] = 54                              #修改第一层元素
print("persons1",persons1)                        #persons1 {'Name': 'Bill', 'age': 30, 'fullname': ['Bill', 'Gates']}
print("persons2",persons2)                        #persons2 {'Name': 'Bill', 'age': 54, 'fullname': ['Bill', 'Gates']}
persons2["fullname"][1] = "Clinton"               #修改第二层元素
print("persons1",persons1)                        #persons1 {'Name': 'Bill', 'age': 30, 'fullname': ['Bill', 'Clinton']}
print("persons2",persons2)                        #persons2 {'Name': 'Bill', 'age': 54, 'fullname': ['Bill', 'Clinton']}
from copy import deepcopy                         #深层复制
persons1 = {"Name":"Bill","age":30,"fullname":["Bill","Gates"]}
persons2 = persons1.copy()
persons3 = deepcopy(persons1)
persons1["fullname"][1] ="Clinton"
print(persons1)                                  #{'Name': 'Bill', 'age': 30, 'fullname': ['Bill', 'Clinton']}
print(persons2)                                  #{'Name': 'Bill', 'age': 30, 'fullname': ['Bill', 'Clinton']}
print(persons3)                                  #{'Name': 'Bill', 'age': 30, 'fullname': ['Bill', 'Gates']}
# fromkeys方法     根据key建立新字典
newdict1 = {}.fromkeys(['name','company','salary'])                #通过列表指定key
print(newdict1)                                                    #{'name': None, 'company': None, 'salary': None}
newdict2 =newdict1.fromkeys(('name','company','salary'))           #通过元组指定key
print(newdict2)                                                    #{'name': None, 'company': None, 'salary': None}
newdict3 = newdict1.fromkeys(['name','company','salary'],'没有值') #第二个参数指定key的默认值
print(newdict3)                                                    #{'name': '没有值', 'company': '没有值', 'salary': '没有值'}
# get方法     从字典中获取key对应的value
dict = {"name":"bill","age":30}                                    # value = dict["salary"] 报错
print(dict.get('x',0))                                             #0, 第二个参数指定key不存在是时返回的数，默认为None
# items方法  返回字典中所有key-value对，可被迭代
dict = {"help":"帮助","bike":"自行车","geek":"极客","china":"中国"}
print(dict.items())
for key_value in dict.items():
    print("key""=",key_value[0],"value","=",key_value[1])
print(("bike","自行车") in dict.items())                            #True
# keys方法    返回字典中所有的的key，可被迭代
print(dict.keys())
for key in dict.keys():
    print(key)
#values方法  返回字典中值的列表
dict = {
    "a":1,
    "b":2,
    "c":2,
    "d":4,
    "e":1
}
print(dict.values())
for value in dict.values():
    print(value)
# pop方法  获取指定key的值，并从字典中弹出这个key-value对
dict = {"c":10,"b":12,"a":40,"x":44}
dict['1'] = 3
dict['5'] = 3
print(dict['b'])
print(dict.pop('b'))                                                 #再输print(dict['b'])，报错，已被弹出
# popitem方法 返回字典中最后一个添加的key-value对，并弹出这个key-value对
for i in range(len(dict)):
    print(dict.popitem())
# setdefault方法 设置key的默认值
dict = {}
print(dict.setdefault("name","Bill"))                                #Bill
print(dict)                                                          #{'name': 'Bill'}
print(dict.setdefault("name","Mike"))                                #Bill
print(dict)                                                          #{'name': 'Bill'}
print(dict.setdefault("age"))                                        #None,默认值为None
# update方法
dict1 = {
    'a':1,
    'b':2,
    'c':3
}
dict2 = {
    'a':4,
    'd':5,
    'c':6
}
dict1.update(dict2)                                                 #用dict2更新dict1
for i in dict1.items():
    print("key = {key}  value = {value}".format(key=i[0],value=i[1]))

